<?php
/** Member queue Model
*
* This is queue class model to access DB and add, delete, reorder the queue of the member
*
* @author  webMAPZ
* @version 39
* @copyright copyright (c) 2011, Boston Univeristy Under BSD
* @todo add function model to redorder the priority list of the customer
*/
class Model_menu extends CI_Model {
    
    function get_menu()
    {
           // The DB query to search for the movies queue of a specific user with a specific user id
            $query_str = "SELECT prod_id ,p.menu_id , m_type,  sku , p_name , slug , route_id, p_description ,
              excerpt , p_price , p_sale_price , p_tax , free_shipping , shippable,
              weight ,in_stock ,related_products , p_images , seo_title , meta text, p_type , options
            FROM `T_PRODUCTS` P ,`T_Menu` M WHERE P.menu_id = M.menu_id and in_stock > 0
            and shippable = 1 and (p_type = '' || p_type is null)
            ORDER BY P.prod_id ASC";
        
            // run the query with $cus_id customer id info to get the results of of movie queue     
            $result = $this->db->query($query_str);
             
            // check if the result contains more than one row of the DB query it will return the results of the movie queue  or  if less than or equal will return FALSE
            if($result->num_rows() > 0)
            {
                return $result;
            }
            else
            {
                return FALSE;
            }
    }
    
    function model_add_queue($cus_id, $prod_id)
    {
            $query_str = "SELECT store_loc_id
                FROM `T_CUSTOMERS` C , `T_DELIVERY_LOCATION` DL WHERE 
                store_c_id = c_id and cus_id = ? ";
             //run the query with the customer id
            $result = $this->db->query($query_str ,$cus_id);
                
            if($result->num_rows() > 0)
            {           
                $cus_loc = $result->row(0)->store_loc_id;
            }
            else
            {
                $cus_loc = 1; // this is the master location
            }
            
            $pending = 'Pending';
            // The DB query to add  the movie to the movie queue
            $query_str = "INSERT INTO `T_CUS_CART` (cus_id, prod_id, store_loc_id, 
                order_date , status) VALUES (?, ?, ?, now(), ?)";
            
            // run the query with the customer id and film id  to insert it to the queue 
            $result = $this->db->query($query_str, array($cus_id, $prod_id, $cus_loc, $pending));
            
            
            $query_str = "Update `T_PRODUCTS` SET in_stock = in_stock - 1 WHERE 
                prod_id = ? ";
            $result2 = $this->db->query($query_str, $prod_id);
            
            if($result && $result2)
            {           
                return true;
            }
            else
            {
                return false;
            }
    }
    
    
    function get_all_menutype()
    {
           $query_str = "SELECT menu_id , m_type
            FROM `T_Menu` WHERE available_flag =1
            ORDER BY menu_id ASC";
            
            $result = $this->db->query($query_str);
             
            if($result->num_rows() > 0)
            {
                return $result->result_array();
            }
            else
            {
                return FALSE;
            }
    }
    
    function model_add_product($sku,  
                    $product_name, $slug, $routid, $product_desc, $excerpt, 
                    $p_price, $p_sale_price, $p_tax, $weight, 
                    $p_stock, $p_image, $options, $p_type, $menuname)
    {

        
         // The DB query to search for a specific username in the DB 
         $query_str = "SELECT menu_id FROM `T_MENU` WHERE M_TYPE = ?";
         // run the query with $username info to get the results of users with the same username
         $result = $this->db->query($query_str, $menuname);

         // check if the result contains more than one row of the DB query it will return the TRUE  or  if less than or equall will return FALSE
         if ($result->num_rows() == 1)
            {
                $menu_id = $result->row(0)->menu_id;

            }
            else
            {
                $menu_id = null;               
            }
            
            
             $query_str = "INSERT INTO `T_PRODUCTS`(menu_id , Sku,P_NAME,SLUG,ROUTE_ID,
                 P_DESCRIPTION, EXCERPT,P_PRICE,P_SALE_PRICE,P_TAX,WEIGHT,IN_STOCK, P_IMAGES, P_TYPE , OPTIONS)
             VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            $result1 =  $this->db->query($query_str, array($menu_id,$sku, 
                $product_name, $slug ,$routid, $product_desc, $excerpt, $p_price, $p_sale_price,
                $p_tax,$weight,$p_stock,$p_image,$p_type,$options,$menuname));     

                
             if ($result && $result1)
                {
                    return true;
                }
                else
                {
                    return FALSE;
                }
    }
    
    
    function model_get_specials_all()
    {
        // The DB query to search for the movies queue of a specific user with a specific user id
            $query_str = "SELECT prod_id ,p.menu_id , m_type,  sku , p_name , slug , route_id, p_description ,
              excerpt , p_price , p_sale_price , p_tax , free_shipping , shippable,
              weight ,in_stock ,related_products , p_images , seo_title , meta text, p_type , options
            FROM `T_PRODUCTS` P ,`T_Menu` M WHERE P.menu_id = M.menu_id and in_stock > 0
            and shippable = 1 and p_type = 'All'
            ORDER BY P.prod_id ASC";
        
            // run the query with $cus_id customer id info to get the results of of movie queue     
            $result = $this->db->query($query_str);
             
            // check if the result contains more than one row of the DB query it will return the results of the movie queue  or  if less than or equal will return FALSE
            if($result->num_rows() > 0)
            {
                return $result->result();;
            }
            else
            {
                return array();
            }
    }
    
    function model_get_specials_breakfast()
    {
        // The DB query to search for the movies queue of a specific user with a specific user id
            $query_str = "SELECT prod_id ,p.menu_id , m_type,  sku , p_name , slug , route_id, p_description ,
              excerpt , p_price , p_sale_price , p_tax , free_shipping , shippable,
              weight ,in_stock ,related_products , p_images , seo_title , meta text, p_type , options
            FROM `T_PRODUCTS` P ,`T_Menu` M WHERE P.menu_id = M.menu_id and in_stock > 0
            and shippable = 1 and p_type = 'Breakfast'
            ORDER BY P.prod_id ASC";
        
            // run the query with $cus_id customer id info to get the results of of movie queue     
            $result = $this->db->query($query_str);
             
            // check if the result contains more than one row of the DB query it will return the results of the movie queue  or  if less than or equal will return FALSE
            if($result->num_rows() > 0)
            {
                return $result->result();;
            }
            else
            {
                return array();
            }
    }
    
    function model_get_specials_launch()
    {
        // The DB query to search for the movies queue of a specific user with a specific user id
            $query_str = "SELECT prod_id ,p.menu_id , m_type,  sku , p_name , slug , route_id, p_description ,
              excerpt , p_price , p_sale_price , p_tax , free_shipping , shippable,
              weight ,in_stock ,related_products , p_images , seo_title , meta text, p_type , options
            FROM `T_PRODUCTS` P ,`T_Menu` M WHERE P.menu_id = M.menu_id and in_stock > 0
            and shippable = 1 and p_type = 'Launch'
            ORDER BY P.prod_id ASC";
        
            // run the query with $cus_id customer id info to get the results of of movie queue     
            $result = $this->db->query($query_str);
             
            // check if the result contains more than one row of the DB query it will return the results of the movie queue  or  if less than or equal will return FALSE
            if($result->num_rows() > 0)
            {
                return $result->result();;
            }
            else
            {
                return array();
            }
    }
    
    function model_get_specials_dinner()
    {
        // The DB query to search for the movies queue of a specific user with a specific user id
            $query_str = "SELECT prod_id ,p.menu_id , m_type,  sku , p_name , slug , route_id, p_description ,
              excerpt , p_price , p_sale_price , p_tax , free_shipping , shippable,
              weight ,in_stock ,related_products , p_images , seo_title , meta text, p_type , options
            FROM `T_PRODUCTS` P ,`T_Menu` M WHERE P.menu_id = M.menu_id and in_stock > 0
            and shippable = 1 and p_type = 'Dinner'
            ORDER BY P.prod_id ASC";
        
            // run the query with $cus_id customer id info to get the results of of movie queue     
            $result = $this->db->query($query_str);
             
            // check if the result contains more than one row of the DB query it will return the results of the movie queue  or  if less than or equal will return FALSE
            if($result->num_rows() > 0)
            {
                return $result->result();;
            }
            else
            {
                return array();
            }
    }
    
    
    function model_get_toppings()
    {
        // The DB query to search for the movies queue of a specific user with a specific user id
            $query_str = "SELECT top_id , top_name , top_desc , top_price , top_pic
            FROM `T_TOPPINGS`
            ORDER BY top_id ASC";
        
            // run the query with $cus_id customer id info to get the results of of movie queue     
            $result = $this->db->query($query_str);
             
            // check if the result contains more than one row of the DB query it will return the results of the movie queue  or  if less than or equal will return FALSE
            if($result->num_rows() > 0)
            {
                return $result->result();;
            }
            else
            {
                return array();
            }
    }
    
}


?>